package demo

class BaseController {

    def dynamicQueryService
    def exportFileService

    /**
     * 导出Excel
     * @return
     * @version 1.0
     */
    def exportExcel() {
        def sysQueryName
        params.startno = 1
        params.endno = 99999999999999999
        params.startTime = URLDecoder.decode(params.startTime, 'UTF-8')
        params.endTime = URLDecoder.decode(params.endTime, 'UTF-8')
        sysQueryName = params.SYS_QUERY_NAME
        params.SYS_QUERY_NAME = sysQueryName
        def dataList = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (dataList.size() > 0) {
            tableHeadList = dataList[0].keySet()
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList, tableHeadList: tableHeadList, startno: params.startno ?: '', endno: params.endno ?: ''])
    }

    /**
     * 导出Excel
     * @return
     * @version 2.0
     */
    def expExcel() {
        def sysQueryName
        params.startno = 1
        params.endno = 99999999999999999
        params.startTime = URLDecoder.decode(params.startTime, 'UTF-8')
        params.endTime = URLDecoder.decode(params.endTime, 'UTF-8')
        sysQueryName = params.SYS_QUERY_NAME
        params.SYS_QUERY_NAME = sysQueryName
        def dataList = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (dataList.size() > 0) {
            tableHeadList = dataList[0].keySet()
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        def result = g.render(template: "exp${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList, tableHeadList: tableHeadList, startno: params.startno ?: '', endno: params.endno ?: ''])
        exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", result)
    }
}